<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let p1  = new Promise((resolve,reject)=>{
            setTimeout(() => {
                resolve(111)
            }, 1000);
        })

        let p2  = new Promise((resolve,reject)=>{
            setTimeout(() => {
                resolve(222)
                // reject("error");
            }, 100);
        })

        let p3  = new Promise((resolve,reject)=>{
            setTimeout(() => {
                resolve(333)
            }, 50);
        })

        // ---------------原有静态方法-------------------------------------------------------
        // Promise.all() 所有的promise对象都是成功状态返回一个 PromiseResult的数组  失败状态 直接返回 PromiseResult
        Promise.all([p1,p2,p3]).then(res =>{
            console.log(res);
        })
        // ---------------重写底层----------------------------------------------------------
        function myAll(arr) {
            let Arr = new Array(arr.length);
            let count = 0;
            return new Promise ((resolve,reject) => {
                arr.forEach((p,index) => {
                    p.then(res => {
                        count++;
                        Arr[index]=res;
                        if (count === 3) {
                            resolve(Arr)
                        }
                    },(err => {
                            reject(err)
                    }))
                });
            })
        }
        myAll([p1,p2,p3]).then(res => {
            console.log(res);
        })
    </script>
</body>
</html>